﻿using Basic.FormCollect;
using Basic.FormExtendService;
using Basic.FormModel;
using Basic.FormModel.DB;
using Basic.FormRemoteModel.Page.Model;
using Basic.FormService.Interface;
using Basic.HrRemoteModel.Power.Model;
using WeDonekRpc.Client;
using WeDonekRpc.Model;

namespace Basic.FormService.lmpl
{
    internal class CustomPageService : ICustomPageService
    {
        private readonly ICustomPageCollect _Service;
        private readonly IPowerService _Power;
        public CustomPageService ( ICustomPageCollect service, IPowerService power )
        {
            this._Power = power;
            this._Service = service;
        }
        public PageAppPower GetPower ( long pageId )
        {
            PowerData data = this._Power.Find(ConstDic.PagePowerLinkType, pageId);
            if ( data == null )
            {
                return null;
            }
            return new PageAppPower
            {
                Description = data.Description,
                Icon = data.Icon,
                Name = data.Name,
                ParentId = data.ParentId,
                SubSystemId = data.SubSystemId,
            };
        }
        public void AddPower ( long pageId, PageAppPower power )
        {
            PageDatum page = this._Service.Get<PageDatum>(pageId);
            this._Power.Save(new PowerSet
            {
                Description = power.Description,
                SubSystemId = power.SubSystemId,
                Icon = power.Icon,
                IsEnable = page.Status == FormRemoteModel.FormPageStatus.启用,
                Name = power.Name,
                PowerType = HrRemoteModel.PowerType.menu,
                RouteName = ConstDic.FormPageRouteName,
                PageParam = new Dictionary<string, string>
                {
                    {"id",pageId.ToString() }
                },
                ParentId = power.ParentId,
                PagePath = ConstDic.FormPagePath,
                RoutePath = ConstDic.FormPageRoutePath,
            }, ConstDic.PagePowerLinkType, pageId);
        }
        public PagingResult<PageDatum> Query ( PageQueryParam query, IBasicPage paging )
        {
            PageDatum[] list = this._Service.Query<PageDatum>(query, paging, out int count);
            return new PagingResult<PageDatum>(list, count);
        }
        public void Delete ( long id )
        {
            DBCustomPage page = this._Service.Get<DBCustomPage>(id);
            this._Service.Delete(page);
            this._Power.Delete(ConstDic.PagePowerLinkType, id);
        }
        public PageDatum Get ( long id )
        {
            return this._Service.Get<PageDatum>(id);
        }
        public bool Set ( long id, PageSet set )
        {
            DBCustomPage page = this._Service.Get<DBCustomPage>(id);
            return this._Service.Set(page, set);
        }
        public long Add ( PageAdd add )
        {
            return this._Service.Add(add);
        }

        public void Enable ( long id )
        {
            DBCustomPage page = this._Service.Get<DBCustomPage>(id);
            if ( this._Service.Enable(page) )
            {
                this._Power.Enable(ConstDic.PagePowerLinkType, id);
            }
        }

        public void Disable ( long id )
        {
            DBCustomPage page = this._Service.Get<DBCustomPage>(id);
            if ( this._Service.Disable(page) )
            {
                this._Power.Stop(ConstDic.PagePowerLinkType, id);
            }
        }
    }
}
